home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / classicos / resta11 / STreeField.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-24  |  5.9 KB  |  317 lines

  1. // STreeField.cpp: Implementierung der Klasse STreeField.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "stdafx.h"
  6. #include "STreeField.h"
  7.  
  8. #include <math.h>
  9.  
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[]=__FILE__;
  13. #define new DEBUG_NEW
  14. #endif
  15.  
  16. #define LEFT_OF(a,x,y) a[x-1][y]
  17. #define RIGHT_OF(a,x,y) a[x+1][y]
  18. #define UPPER_OF(a,x,y) a[x][y-1]
  19. #define LOWER_OF(a,x,y) a[x][y+1]
  20.  
  21. //////////////////////////////////////////////////////////////////////
  22. // Konstruktion/Destruktion
  23. //////////////////////////////////////////////////////////////////////
  24.  
  25. STreeField::STreeField()
  26. {
  27.     dragging = -1;
  28.     lastPos[0] = 0;
  29.     lastPos[1] = 0;
  30. }
  31.  
  32. STreeField::~STreeField()
  33. {
  34.  
  35. }
  36.  
  37. void STreeField::create(STexture *baum, STexture *stamm,SSoundSystem* sound)
  38. {
  39.     soundSystem = sound;
  40.  
  41.     for (int k = 0; k < MATRIX_SIZE; k += 1)
  42.     {
  43.         for (int j = 0; j < MATRIX_SIZE; j += 1)
  44.         {
  45.             matrix[k][j] = false;
  46.         }
  47.     }
  48.  
  49.     //Mittelpunkt
  50. //    matrix[3][3] = false;
  51.  
  52.     int i = 0;
  53.     int* matrixPosition;
  54.  
  55.     for (float x = -3.0f*SPACE; x <= 3.0f*SPACE; x += SPACE)
  56.     {
  57.         for (float y = -3.0f*SPACE; y <= 3.0f*SPACE; y += SPACE)
  58.         {
  59.             if ( (x == -SPACE) || (x == SPACE) || (x == 0.0f) ||
  60.                  (y == -SPACE) || (y == SPACE) || (y == 0.0f))
  61.             {
  62.                 if (!((x == 0.0f) && (y == 0.0f)))
  63.                 {
  64.                     trees[i].create(baum,stamm);
  65.                     matrixPosition = trees[i].resolveFinalPos(x,y);
  66.                     trees[i].setFinalPos(matrixPosition[0],matrixPosition[1]);
  67.                     //trees[i].setFinalPos(matrixPosition[0],matrixPosition[1]);
  68.  
  69.  
  70.                     matrix[ matrixPosition[0] ][ matrixPosition[1] ]  = true;
  71.  
  72.                     
  73.                     i += 1;
  74.                     
  75.                     if (i >= NUM_TREES)
  76.                     {
  77.                         return;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84. }
  85.  
  86. void STreeField::update(float frametime)
  87. {
  88.     for (int i = 0; i < NUM_TREES; i += 1)
  89.     {
  90.         trees[i].update(frametime);
  91.     }
  92. }
  93.  
  94. bool STreeField::click(float x, float y)
  95. {
  96.     for (int i = 0; i < NUM_TREES; i += 1)
  97.     {
  98.         if (trees[i].click(x,y))
  99.         {
  100.             lastPos[0] = trees[i].getXMatrixPos();
  101.             lastPos[1] = trees[i].getYMatrixPos();
  102.  
  103.             dragging = i;
  104.             return true;
  105.         }
  106.     }
  107.  
  108.     return false;
  109. }
  110.  
  111. void STreeField::move(float x, float y)
  112. {
  113.     if (dragging != -1)
  114.     {
  115.         trees[dragging].setPosition(x,y,0.0f);
  116.     }
  117. }
  118.  
  119. void STreeField::release(float x, float y)
  120. {
  121.     if (dragging == -1)
  122.     {
  123.         return;
  124.     }
  125.  
  126.     int* possibility = trees[dragging].resolveFinalPos(x,y);
  127.     
  128.  
  129.     //TRACE2("RESOLVED: %d,%d\n",possibility[0],possibility[1]);
  130.  
  131.  
  132.     if (possibility[0] != -1 && possibility[1] != -1 &&
  133.         !matrix[ possibility[0] ][ possibility[1] ])
  134.     {
  135.         //TESTEN OB FUNKTIONIERT 
  136.         // + ⁿbersprungenen Baum zerst÷ren
  137.  
  138.         int xLength = lastPos[0]-possibility[0];
  139.         int yLength = lastPos[1]-possibility[1];
  140.  
  141.         //*****************
  142.         // TESTs
  143.         // 1.Der Baum muss zwei schritte bewegt worden sein
  144.         //   und nicht diagonal xLength=yLength
  145.  
  146.         if ((fabs(xLength) == 2 && fabs(xLength) < 3) ||
  147.            (fabs(yLength) == 2 && fabs(yLength) < 3) &&
  148.             (fabs(xLength) !=  fabs(yLength)))
  149.         {
  150.             //Mittelpunkt finden (Baum dazwischen)
  151.  
  152.             int xCenter = (lastPos[0]+possibility[0])/2;
  153.             int yCenter = (lastPos[1]+possibility[1])/2;
  154.  
  155.             if (matrix[xCenter][yCenter] == true)
  156.             {
  157.                 matrix[lastPos[0]][lastPos[1]] = false;
  158.                 matrix[possibility[0]][possibility[1]] = true;
  159.                 matrix[xCenter][yCenter] = false;
  160.  
  161.                 trees[dragging].setFinalPos(possibility[0],possibility[1]);
  162.                 trees[dragging].setState(STATE_SHOWN);
  163.                 
  164.             
  165.                 for (int i = 0; i < NUM_TREES; i += 1)
  166.                 {
  167.                     if (trees[i].getXMatrixPos() == xCenter &&
  168.                         trees[i].getYMatrixPos() == yCenter &&
  169.                         trees[i].getState() != STATE_HIDDEN && 
  170.                         trees[i].getState() != STATE_DESTROYING)
  171.                     {
  172.                         trees[i].setState(STATE_DESTROYING);
  173.                         break;
  174.                     }
  175.                 }
  176.  
  177.             
  178.                 //***********************************************
  179.  
  180.                 dragging = -1;
  181.                 
  182.                 //***SOUND***
  183.  
  184.                 soundSystem->playSample(SAMPLE_RELEASE);
  185.  
  186.                 return;
  187.             }
  188.  
  189.         }
  190.     }
  191.     
  192.     
  193.     trees[dragging].setFinalPos(lastPos[0],lastPos[1]);
  194.     trees[dragging].setState(STATE_SHOWN);
  195.     
  196.     dragging = -1;
  197. }
  198.  
  199. bool STreeField::isDone()
  200. {
  201.     for (int i = 0; i < MATRIX_SIZE; i += 1)
  202.     {
  203.         for (int j = 0; j < MATRIX_SIZE; j += 1)
  204.         {
  205.             if (matrix[i][j] /*&& 
  206.                 !((i < 2 && j < 2) ||
  207.                   (i > 4 && j < 2) ||
  208.                   (i < 2 && j > 4) ||
  209.                   (i > 4 && j > 4) ) */)
  210.             {
  211.                 //LINKS
  212.                 if (i != 0 && i != MATRIX_SIZE-1)
  213.                 {
  214.                     if (j < 2 || j > 4)
  215.                     {
  216.                         if ( i+1 < 5)
  217.                         {
  218.                             if (LEFT_OF(matrix,i,j) && !RIGHT_OF(matrix,i,j))
  219.                             {
  220.                                 return false;
  221.                             }
  222.                         }
  223.                     }
  224.                     else
  225.                     {
  226.                         if (LEFT_OF(matrix,i,j) && !RIGHT_OF(matrix,i,j))
  227.                         {
  228.                             return false;
  229.                         }
  230.                     }
  231.                         
  232.                 }
  233.  
  234.                 //RECHTS
  235.                 if (i != 0 && i != MATRIX_SIZE-1)
  236.                 {    
  237.                     if (j < 2 || j > 4)
  238.                     {
  239.                         if (i-1 >= 2)
  240.                         {
  241.                             if (RIGHT_OF(matrix,i,j) && !LEFT_OF(matrix,i,j))
  242.                             {
  243.                                 return false;
  244.                             }
  245.                         }
  246.                     }
  247.                     else
  248.                     {
  249.                         if (RIGHT_OF(matrix,i,j) && !LEFT_OF(matrix,i,j))
  250.                         {
  251.                             return false;
  252.                         }
  253.                     }
  254.                 }
  255.  
  256.                 //OBEN
  257.                 if (j != 0 && j != MATRIX_SIZE-1)
  258.                 {
  259.                     if (i < 2 || i > 4)
  260.                     {
  261.                         if (j+1 < 5)
  262.                         {
  263.                             if (UPPER_OF(matrix,i,j) && !LOWER_OF(matrix,i,j))
  264.                             {
  265.                                 return false;
  266.                             }
  267.                         }
  268.                     }
  269.                     else
  270.                     {
  271.                         if (UPPER_OF(matrix,i,j) && !LOWER_OF(matrix,i,j))
  272.                         {
  273.                             return false;
  274.                         }
  275.                     }
  276.                 }
  277.  
  278.                 //UNTEN
  279.                 if (j != 0 && j != MATRIX_SIZE-1)
  280.                 {
  281.                     if (i < 2 || i > 4)
  282.                     {
  283.                         if (j-1 >= 2)
  284.                         {
  285.                             if (LOWER_OF(matrix,i,j) && !UPPER_OF(matrix,i,j))
  286.                             {
  287.                                 return false;
  288.                             }
  289.                         }
  290.                         
  291.                     }
  292.                 }
  293.             }
  294.         }
  295.     }
  296.  
  297.     return true;
  298. }
  299.  
  300. int STreeField::treesLeft()
  301. {
  302.     int result = 0;
  303.  
  304.     for (int i = 0; i < MATRIX_SIZE; i += 1)
  305.     {
  306.         for (int j = 0; j < MATRIX_SIZE; j += 1)
  307.         {
  308.             if (matrix[i][j])
  309.             {
  310.                  result++;
  311.             }
  312.         }
  313.     }
  314.  
  315.     return result;
  316. }
  317.